<template>
  <contentModel v-if="singleProShow" :title="'编辑'" @close="handleClose">
    <template #footer>
      <a-button type="primary" @click="handleSubmit">保存</a-button></template
    >
    <a-form
      ref="formRef"
      :model="form"
      auto-label-width
      :rules="formRules"
      @submit="handleSubmit"
      ><a-row :gutter="16">
        <a-col :span="8"
          ><a-form-item field="singleSourceReason" label="单一来源原因：">
            <dict-select
              v-model="form.singleSourceReason"
              allow-clear
              dict-type="single_cause_type"
              placeholder="请选择"
            ></dict-select> </a-form-item
        ></a-col>
        <a-col :span="8">
          <a-form-item field="singleSourceDesc" label="单一来源描述:">
            <a-input
              v-model="form.singleSourceDesc"
              placeholder=""
            /> </a-form-item
        ></a-col>
        <a-col :span="8"
          ><a-form-item field="supplierCode" label="供应商编码：">
            <inputModal
              v-model="form.supplierCode"
              :label-value="form.supplierCode"
              :value-name="'supplierCode'"
              :title="'供应商编码'"
              :type="'supplyName'"
              @handle-ok="selectSupplier" /></a-form-item
        ></a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8"
          ><a-form-item field="supplierName" label="供应商名称：">
            <a-input
              v-model="form.supplierName"
              placeholder=""
              disabled
            /> </a-form-item
        ></a-col>
        <a-col :span="8"
          ><a-form-item field="purchaseState" label="是否采购方式变更">
            <dict-select
              v-model="form.purchaseState"
              allow-clear
              dict-type="YN"
              placeholder="请选择"
            ></dict-select></a-form-item
        ></a-col>
        <a-col :span="8"
          ><a-form-item field="taxRateCode" label="税率：">
            <dict-select
              v-model="form.taxRateCode"
              allow-clear
              dict-type="taxRate"
              placeholder="请选择"
            ></dict-select> </a-form-item
        ></a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8"
          ><a-form-item field="mdsUintRate" label="转换系数：">
            <a-input v-model="form.mdsUintRate" placeholder="" /> </a-form-item
        ></a-col>
        <a-col :span="8"
          ><a-form-item field="inputType" label="录入类型：">
            <dict-select
              v-model="form.inputType"
              allow-clear
              dict-type="inputType"
              placeholder="请选择"
            ></dict-select></a-form-item
        ></a-col>
        <a-col :span="8">
          <a-form-item field="currencyType" label="币种：">
            <dict-select
              v-model="form.currencyType"
              allow-clear
              dict-type="currencyType"
              placeholder="请选择"
            ></dict-select>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="8">
          <a-form-item field="pmsInvQty" label="计划数量(库存):">
            <a-input v-model="form.pmsInvQty" placeholder="" /> </a-form-item
        ></a-col> -->
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8"
          ><a-form-item field="pmsMdsUnitCode" label=" 计量单位（采购）：">
            <dict-select
              v-model="form.pmsMdsUnitCode"
              allow-clear
              dict-type="unitOfMeasurement"
              placeholder="请选择"
            ></dict-select> </a-form-item
        ></a-col>
        <a-col :span="8"
          ><a-form-item field="taxPrice" label="含税单价：">
            <a-input
              v-model="form.taxPrice"
              placeholder=""
              :disabled="form.inputType === '0'"
            /> </a-form-item
        ></a-col>
        <a-col :span="8"
          ><a-form-item field="pmsEstimatePrice" label="预估价：">
            <a-input
              v-model="form.pmsEstimatePrice"
              placeholder=""
            /> </a-form-item
        ></a-col>
      </a-row>
      <a-row :gutter="16">
        <a-col :span="8"
          ><a-form-item field="exTaxPrice" label="不含税单价(采购)：">
            <a-input
              v-model="form.exTaxPrice"
              placeholder=""
              :disabled="form.inputType === '1'"
            /> </a-form-item
        ></a-col>

        <!-- <a-col :span="8"
          ><a-form-item field="invPurQty" label="计划数量(采购):">
            <a-input v-model="form.invPurQty" placeholder="" /> </a-form-item
        ></a-col> -->
      </a-row>
    </a-form>
  </contentModel>
</template>

<script lang="ts" setup>
  import dictSelect from '@/common/components/dict-select/index.vue';
  import inputModal from '@/common/components/search-modal/input-modal.vue';
  import { defineAsyncComponent, ref } from 'vue';
  import {
    batchUpdateForSimple, // 单一来源编辑提交
  } from '@/api/supplier/selective-quotient';
  import { Message } from '@arco-design/web-vue';

  const contentModel = defineAsyncComponent(
    () => import('@/common/components/content-model/index.vue')
  );
  const formRef = ref();
  const formRules = ref({
    singleSourceReason: [
      {
        required: true,
        message: '单一来源原因不能为空',
        trigger: 'change',
      },
    ],
    singleSourceDesc: [
      {
        required: true,
        message: '单一来源描述不能为空',
        trigger: 'change',
      },
    ],
    supplierCode: [
      {
        required: true,
        message: '供应商编码不能为空',
        trigger: 'change',
      },
    ],
    supplierName: [
      {
        required: true,
        message: '供应商名称不能为空',
        trigger: 'change',
      },
    ],
    pmsMdsUnitCode: [
      {
        required: true,
        message: '计量单位不能为空',
        trigger: 'change',
      },
    ],
    mdsUintRate: [
      {
        required: true,
        message: '请输入转换系数',
        trigger: 'change',
      },
    ],
    currencyType: [
      {
        required: true,
        message: '请选择币种',
        trigger: 'change',
      },
    ],
    inputType: [
      {
        required: true,
        message: '请输入录入类型',
        trigger: 'change',
      },
    ],
    taxRateCode: [
      {
        required: true,
        message: '请选择税率',
        trigger: 'change',
      },
    ],
  });
  defineProps({
    singleProShow: { type: Boolean, default: false },
  });
  const emit = defineEmits(['update:singleProShow', 'refresh', 'handleClose']);
  // const emit = defineEmits(['handleClose']);
  // const form = computed(() => {
  //   return props.formData;
  // });
  const selectSupplier = (val) => {
    console.log(val);
    form.value.supplierName = val.supplierChinese;
    formRef.value.validateField('supplierCode');
  };
  const init = (val) => {
    form.value = val;
    form.value.purchaseState = val.purchaseState.toString();
  };
  const form = ref({
    mdsUintRate: '',
    inputType: '',
    pmsInvQty: '',
    pmsMdsUnitCode: '',
    taxPrice: '',
    pmsEstimatePrice: '',
    exTaxPrice: '',
    taxRateCode: '',
    invPurQty: '',
    currencyType: '',
    singleSourceReason: '',
    singleSourceDesc: '',
    supplierCode: '',
    supplierName: '',
  });
  const handleClose = () => {
    emit('update:singleProShow', false);
  };
  const handleSubmit = async () => {
    formRef.value.validate(async (validate) => {
      if (!validate) {
        const { data } = await batchUpdateForSimple([form.value]);
        if (data) {
          Message.success('编辑成功');
          handleClose();
          emit('handleClose');
        }
      }
    });
  };
  defineExpose({ init });
</script>

<style lang="less" scoped>
  /deep/ .arco-picker {
    width: 100%;
  }

  .view-background {
    padding: 16px;
    background: #f7f9fc;
  }
</style>
